package com.xnfh.planting.viewmodel

import com.huantansheng.easyphotos.models.album.entity.Photo
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.base.BaseViewModel
import com.xnfh.framework.bean.RespStateData
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/18 16:29
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/18 16:29
 * @UpdateRemark: 更新说明
 */
class AddXunTianViewModel : BaseViewModel() {


    private val addXunTianViewModelRes = AddXunTianViewModelRes()

    val addXunTianData = RespStateData<Any>()

    /**
     * 添加巡田记录
     */
    fun requestAddRecord(
        photoList: ArrayList<Photo>,
        fieldId: String?,
        plantCropId: Long?,
        xtData: String,
        remark: String
    ) = launch {
        //1.创建MultipartBody.Builder对象
        val builder = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
        photoList.forEach {
            val file = File(it.path)
            val body: RequestBody =
                RequestBody.create(MediaType.parse("multipart/form-data"), file) //表单类型
            builder.addFormDataPart("file",file.name,body)
        }
        builder.addFormDataPart("fieldId",fieldId.toString())
        builder.addFormDataPart("cropId",plantCropId.toString())
        builder.addFormDataPart("farmingTime",xtData)
        if(remark.isNotBlank()){
            builder.addFormDataPart("remark",remark)
        }
        val parts = builder.build().parts()
        addXunTianViewModelRes.requestAddRecord(parts,addXunTianData)
    }

}